export const fetchInvoiceList = async (pageNum, pageSize,filterForm) => {
  try {
    let url = `/api/invoices?pageNum=${pageNum}&limit=${pageSize}`;
    if(filterForm.invoice_code){
      url += `&invoice_code=${filterForm.invoice_code}`
    }
    if(filterForm.buyer_name){
      url += `&buyer_name=${filterForm.buyer_name}`
    }
    if(filterForm.seller_name){
      url += `&seller_name=${filterForm.seller_name}`
    }
    if(filterForm.issue_date && filterForm.issue_date.length>1){
      url += `&start_date=${filterForm.issue_date[0]}&end_date=${filterForm.issue_date[1]}`
    }
    if(filterForm.license_plate){
      url += `&license_plate=${filterForm.license_plate}`
    }
    const response = await fetch(url);
    return await response.json();
  } catch (error) {
    console.error('Error fetching invoice data:', error);
    throw error;
  }
};

export const createInvoice = async (invoiceData) => {
  try {
    const response = await fetch('/api/invoices', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(invoiceData),
    });
    return await response.json();
  } catch (error) {
    console.error('Error creating invoice:', error);
    throw error;
  }
};

export const editInvoice = async (invoice_id,invoiceData) => {
  console.log(invoiceData)
  try {
    const response = await fetch('/api/invoices/'+invoice_id, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(invoiceData),
    });
    return await response.json();
  } catch (error) {
    console.error('Error creating invoice:', error);
    throw error;
  }
};

export const deleteInvoice = async (invoice_id) => {
  try {
    const response = await fetch('/api/invoices/'+invoice_id, {
      method: 'DELETE',
      headers: {
        'Content-Type': 'application/json',
      },
    });
    return await response.json();
  } catch (error) {
    console.error('Error creating invoice:', error);
    throw error;
  }
};

export const parseInvoicePDF = async (formData) => {
  try {
    const response = await fetch('/api/invoice/parse', {
      method: 'POST',
      body: formData,
    });
    return await response.json();
  } catch (error) {
    console.error('Error parsing invoice PDF:', error);
    throw error;
  }
};

export const parseInvoiceZIP = async (formData) => {
  try {
    const response = await fetch('/api/invoice/zipparse', {
      method: 'POST',
      body: formData,
    });
    return await response.json();
  } catch (error) {
    console.error('Error parsing invoice ZIP:', error);
    throw error;
  }
};


export const createInvoiceByZip = async (fileList) => {
  try {
    const response = await fetch('/api/invoicesByZip', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(fileList),
    });
    return await response.json();
  } catch (error) {
    console.error('Error creating invoiceByZip:', error);
    throw error;
  }
};

export const reparseInvoiceApi = async (invoice_id) => {
  try {
    const response = await fetch('/api/reparseInvoice', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify([invoice_id]),
    });
    return await response.json();
  } catch (error) {
    console.error('Error creating reparseInvoice:', error);
    throw error;
  }
};